package com.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger2配置文件
 * @author biao.yang
 *
 */
@Configuration
@EnableSwagger2
public class Swagger2 {
	/**
	 * 通过 createRestApi函数来构建一个DocketBean
	 * @return
	 */
    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<>();

        ParameterBuilder contentType = new ParameterBuilder();
        contentType.name("Content-Type").defaultValue("application/json")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(true).build();

        ParameterBuilder token = new ParameterBuilder();
        token.name("token").defaultValue("").description("认证token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();

        pars.add(contentType.build());
        pars.add(token.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars).apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("钱包管理后台")
                .license("Apache-2.0")
                .description("钱包管理后台-Api接口")
                .contact(new Contact("钱包APP","https://xxx.xxx.com","xxx"))
                .version("1.0.x")
                .build();
    }
}
